/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.Reportes.Vista;


import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import pe.edu.pucp.Modelo.Actividad;
import pe.edu.pucp.Modelo.Log;
import pe.edu.pucp.Modelo.Objeto;
import pe.edu.pucp.Modelo.Permiso;
import pe.edu.pucp.Util.Constante;
import pe.edu.pucp.Util.ValHelper;

/**
 *
 * @author jorge
 */
public class ReporteAuditoriaGUI extends javax.swing.JPanel {
    
    private JFrame padre;
    /**
     * Creates new form ReporteAuditoriaGUI
     */
    public ReporteAuditoriaGUI() {
        initComponents();
        ((JTextField)dtcFechaInicio.getDateEditor()).setEditable(false);
        ((JTextField)dtcFechaFin.getDateEditor()).setEditable(false);
        dtcFechaInicio.setMaxSelectableDate(new Date());
        dtcFechaFin.setMaxSelectableDate(new Date());
    }

    public JFrame getPadre() { return padre; }    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cmbAccion = new javax.swing.JComboBox();
        btnBuscar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        btnGenerarPDF = new javax.swing.JButton();
        cmbObjeto = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        dtcFechaInicio = new com.toedter.calendar.JDateChooser();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        dtcFechaFin = new com.toedter.calendar.JDateChooser();
        txtNombre = new javax.swing.JTextField();
        pnLog = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblResultados = new javax.swing.JTable();

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        setMaximumSize(new java.awt.Dimension(764, 700));
        setMinimumSize(new java.awt.Dimension(764, 700));
        setPreferredSize(new java.awt.Dimension(764, 700));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Buscar", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu", 0, 14))); // NOI18N

        jLabel1.setText("Usuario:");

        jLabel2.setText("Accion:");

        cmbAccion.setMaximumSize(new java.awt.Dimension(40, 27));

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/search.png"))); // NOI18N

        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/refresh.png"))); // NOI18N

        btnGenerarPDF.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/address_book.png"))); // NOI18N

        jLabel4.setText("Objeto:");

        //dtcFechaInicio.setEnabled(false);
        dtcFechaInicio.setVerifyInputWhenFocusTarget(false);
        dtcFechaInicio.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dtcFechaInicioPropertyChange(evt);
            }
        });

        jLabel5.setText("Fecha inicio: ");

        jLabel6.setText("Fecha fin: ");

        //dtcFechaFin.setEnabled(false);
        dtcFechaFin.setVerifyInputWhenFocusTarget(false);
        dtcFechaFin.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dtcFechaFinPropertyChange(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cmbAccion, 0, 260, Short.MAX_VALUE)
                    .addComponent(cmbObjeto, 0, 260, Short.MAX_VALUE)
                    .addComponent(dtcFechaInicio, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                    .addComponent(dtcFechaFin, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                    .addComponent(txtNombre))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 161, Short.MAX_VALUE)
                .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnGenerarPDF, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(cmbAccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel2)
                    .addComponent(cmbObjeto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(dtcFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(dtcFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(0, 33, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnGenerarPDF, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pnLog.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Resultado", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Ubuntu", 0, 14))); // NOI18N

        tblResultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "USUARIO", "ACCION", "OBJETO", "FECHA"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblResultados.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tblResultados.getTableHeader().setResizingAllowed(false);
        tblResultados.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblResultados);
        tblResultados.getColumnModel().getColumn(0).setResizable(false);
        tblResultados.getColumnModel().getColumn(1).setResizable(false);
        tblResultados.getColumnModel().getColumn(2).setResizable(false);
        tblResultados.getColumnModel().getColumn(3).setResizable(false);

        javax.swing.GroupLayout pnLogLayout = new javax.swing.GroupLayout(pnLog);
        pnLog.setLayout(pnLogLayout);
        pnLogLayout.setHorizontalGroup(
            pnLogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnLogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 704, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnLogLayout.setVerticalGroup(
            pnLogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnLogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnLog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pnLog, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void dtcFechaInicioPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dtcFechaInicioPropertyChange
        if ("date".equals(evt.getPropertyName())) {
            dtcFechaFin.setMinSelectableDate((Date) evt.getNewValue());
        }        
    }//GEN-LAST:event_dtcFechaInicioPropertyChange

    private void dtcFechaFinPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dtcFechaFinPropertyChange
        if ("date".equals(evt.getPropertyName())) {
            dtcFechaInicio.setMaxSelectableDate((Date) evt.getNewValue());                              
        }          
    }//GEN-LAST:event_dtcFechaFinPropertyChange

    public Date getFechaInicio() { 
        return dtcFechaInicio.getDate();     
    }
    
    public Date getFechaFin() { 
        return dtcFechaFin.getDate(); 
    }
    
    public String getUsuario(){ 
        return txtNombre.getText().toUpperCase(); 
    }   
    
    public int getAccion(){ 
        if(cmbAccion.getSelectedIndex() > 0)
            return cmbAccion.getSelectedIndex() - 1;
        else return -1; 
    }
    
    public int getObjeto(){ 
        if(cmbObjeto.getSelectedIndex() > 0)
            return cmbObjeto.getSelectedIndex() -1; 
        else return -1;
    }
    
    
    
    public void mostrarPermisos(ArrayList<Permiso> list){            
        if (!list.isEmpty()){                                
            btnBuscar.setEnabled(buscarPermiso(list,Constante.P_SEG_REP_BUSCAR)==1);
            btnGenerarPDF.setEnabled(buscarPermiso(list,Constante.P_SEG_REP_GENERAR)==1);
        }
    }    
  
    public int buscarPermiso(ArrayList<Permiso> list, int idel){
        int per=-1;
        for (Permiso opc : list){
            if (opc.getElemento().getIdelemento()==idel){ per =opc.getPermiso(); }
        }
        return per;
    }     

//    public void setpath(String fileName) {
//        this.txtPath.setText(fileName);
//    }
//    
//    public String getpath() {
//        return this.txtPath.getText();
//    }
    
    public void addLimpiarListener(ActionListener listener) { 
        btnLimpiar.addActionListener(listener); 
    }
        
    public void addBuscarListener(ActionListener ml) { 
        btnBuscar.addActionListener(ml); 
    }
    
    public void addGenerarListener(ActionListener ml) { 
        btnGenerarPDF.addActionListener(ml); 
    }         
    
//    public void addCarpeta(ActionListener ml) {
//        btnPath.addActionListener(ml); 
//    }
//    
    public void llenarComboObjeto(ArrayList<Objeto> lista){
        cmbObjeto.removeAllItems();
        cmbObjeto.addItem(Constante.DEFAULT_OPCION);   
        if(lista!=null){
            for (Objeto opc : lista){
                cmbObjeto.addItem(opc.getDescripcion());
            }
        }
    }

    public void llenarComboAccion(ArrayList<Actividad> lista){
        cmbAccion.removeAllItems();
        cmbAccion.addItem(Constante.DEFAULT_OPCION);   
        if(lista!=null){
            for (Actividad opc : lista){
                cmbAccion.addItem(opc.getDescripcion());
            }
        }
    }       
    
    public void llenaGrilla(ArrayList<Log> lista){        
        if(lista!=null){ 
            ((DefaultTableModel) this.tblResultados.getModel()).setRowCount(0);
            for (int i = 0; i<lista.size();i++){
                ((DefaultTableModel) tblResultados.getModel()).setRowCount(tblResultados.getRowCount()+1);               
                tblResultados.setValueAt(lista.get(i).getUsuario().getNombre()+" "+lista.get(i).getUsuario().getAppaterno(),i,0);
                tblResultados.setValueAt(lista.get(i).getActividad().getDescripcion(),i,1);
                tblResultados.setValueAt(lista.get(i).getObjeto().getDescripcion(),i,2);
                tblResultados.setValueAt(lista.get(i).getFechareg(),i,3);    
            }
            pnLog.setBorder(javax.swing.BorderFactory.createTitledBorder("Logs ("+lista.size() + ")"));            
        } 
        else {
            pnLog.setBorder(javax.swing.BorderFactory.createTitledBorder("Logs"));            
        }        
      
    }  
    
    public void LimpiarDatos(){
        txtNombre.setText("");
        cmbAccion.setSelectedIndex(0);
        cmbObjeto.setSelectedIndex(0);
        dtcFechaInicio.setDate(null);
        dtcFechaFin.setDate(null);
    }    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnGenerarPDF;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JComboBox cmbAccion;
    private javax.swing.JComboBox cmbObjeto;
    private com.toedter.calendar.JDateChooser dtcFechaFin;
    private com.toedter.calendar.JDateChooser dtcFechaInicio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnLog;
    private javax.swing.JTable tblResultados;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables
}
